<?php
namespace app\apps\controller;
use framework\ext\Check;
/* *
 * 应用管理
 * */
class indexController extends \app\base\controller\AdminController{
	//列表
	public function index(){
        $act = $_GET['act'];
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result =  model('index')->_delete($fid);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败');
            }
        }else{
            $page = form('get.page',0,intval);
            $this->list = model('index')->pager($page,20)->_select();
            $this->pager = $this->getPage(model('index')->pager);
            $this->display();
        }
	}
	//扩展应用
	public function edit(){
        if($this->isPost()){
            $msg = Check::rule(
                    array(check::must($_POST['app']),'APPID必须填写'),
                    array(check::must($_POST['appname']),'应用名称必须填写'),
                    array(check::must($_POST['var']),'最新版本必须填写'),
                    array(check::must($_POST['down']),'下载地址必须填写'),
                    array(check::must($_POST['uptime']),'更新时间必须填写'),
                    array(check::must($_POST['info']),'更新描述必须填写')
                    );
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $fid = form('post.fid',0,intval);
            $data = form('post',array('fid'));
            $data['uptime'] = strtotime($data['uptime']);
            if($fid){
                $condition['id'] = $fid;
                $rel = model('index')->_update($data,$condition);
            }else{
                $rel = model('index')->_update($data);
            }
            if($rel){
                $this->jsonMsg('增加或编辑成功',1,url('index/index'));
            }else{
                $this->jsonMsg('增加或编辑失败');
            }
        }else{
            $this->fid = form('get.id',0,intval);
            $this->info = model('index')->_find(array('id'=>$this->fid));
            $this->display(); 
        }
	}
	//示例列表
	public function vip(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result =  model('vip')->delete('id ='.$fid);
            if($result){
                echo 1;
            }else{
                echo '删除失败';
            } 
        }else{
            $listRows = 20;
            $url = url('index/vip',array('page'=>'{page}'));
            $allcount = model('vip')->count();
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->list = model('vip')->select('',null,null,$limit); 
            $this->display();
        }
	}
	//扩展应用
	public function vipedit(){
        if($this->isPost()){
            $msg = Check::rule(
                    array(check::must($_POST['app']),'APPID必须填写'),
                    array(check::must($_POST['appname']),'应用名称必须填写'),
                    array(check::must($_POST['name']),'网站名称必须填写'),
                    array(check::must($_POST['url']),'授权网址必须填写'),
                    array(check::must($_POST['vipuser']),'授权持有人必须填写'),
                    array(check::must($_POST['viptime']),'授权起始时间必须填写'),
                    array(check::must($_POST['services']),'服务起止日期必须填写')
                    );
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $fid = intval($_POST['fid']);
            $data = postinput($_POST,array('fid'));
            if($fid){
                $info = model('vip')->update('id='.$fid,$data);
            }else{
                $info = model('vip')->insert($data);
            }
            if($info){
                $this->jsonMsg('增加或编辑成功',1);
            }else{
                $this->jsonMsg('增加或编辑失败');
            }
        }else{
            $this->fid = intval($_GET['id']);
            $this->info = model('vip')->find('id = '. $this->fid);
            $this->display(); 
        }
	}
}